package one.stand.controller;

import com.github.pagehelper.PageHelper;
import com.jmp.api.admin.dto.*;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.ArticleApi;
import com.jmp.base.LabelApi;
import com.jmp.base.dto.LabelRelateRequest;
import com.jmp.base.dto.LabelUpdateRequest;
import com.jmp.feign.LabelFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageResponse2;
import one.stand.mapper.CompanyApiAdminMapper;
import one.stand.mapper.CompanyCustomerLableMapperExt;
import one.stand.model.CompanyCustomerLable;
import one.stand.model.ResultModel;
import one.stand.util.AssertUtil;
import one.stand.util.RequestContextHolder;
import one.stand.util.SqlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

@Api(description = "企业标签")
@RestController
@RequestMapping("adminApi/label")
public class LabelController {
    @Autowired
    private LabelFeign labelFeign;

    @Autowired
    private CompanyApiAdminMapper companyApiAdminMapper;
    @Autowired
    private CompanyCustomerLableMapperExt companyCustomerLableMapperExt;

    @ApiOperation(value = "列表")
    @PostMapping("")
    @SecurityAuthCheck
    public ResultModel<LabelListResponse> list(@RequestBody LabelListRequest request) {
        String searchNameLike = SqlUtil.like(request.getSearchName());
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<CompanyCustomerLable> labels = companyApiAdminMapper.selectLabel(ArticleApi.DEFAULT_COMPANYID, searchNameLike);
        PageResponse2<LabelListResponse.Label> labelPage = ConvertUtils.page2(labels, label -> LabelListResponse.Label.builder()
                .id(label.getLableId())
                .labelName(label.getLableName())
                .companyNum(companyCustomerLableMapperExt.selectTypeIdCount(label.getLableId(), LabelApi.LABEL_TYPE_COMPANY))
                .build());

        LabelListResponse response = LabelListResponse.builder()
                .labels(labelPage)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation(value = "保存")
    @PostMapping("save")
    @SecurityAuthCheck
    public ResultModel save(@RequestBody LabelSaveRequest request) {
        ResultModel resultModel;
        if (Objects.nonNull(request.getLabelId())) {
            LabelUpdateRequest updateRequest = LabelUpdateRequest.builder()
                    .lableId(request.getLabelId())
                    .lableName(request.getLabelName())
                    .build();
            resultModel = labelFeign.update(updateRequest);
        } else {
            com.jmp.base.dto.LabelSaveRequest saveRequest = com.jmp.base.dto.LabelSaveRequest.builder()
                    .lableName(request.getLabelName())
                    .userId(RequestContextHolder.getRequestContext().getUser().getUserId())
                    .build();
            resultModel = labelFeign.save(saveRequest);
        }
        AssertUtil.isTrue(resultModel);
        return ResultModel.success();
    }

    @ApiOperation(value = "删除")
    @PostMapping("delete")
    @SecurityAuthCheck
    public ResultModel delete(@RequestBody LabelDeleteRequest request) {
        com.jmp.base.dto.LabelDeleteRequest deleteRequest = com.jmp.base.dto.LabelDeleteRequest.builder()
                .lableId(request.getLabelId())
                .build();
        ResultModel resultModel = labelFeign.delete(deleteRequest);
        AssertUtil.isTrue(resultModel);
        return ResultModel.success();
    }

    @ApiOperation(value = "企业编辑标签")
    @PostMapping("company")
    @SecurityAuthCheck
    public ResultModel<LabelCompanyResponse> list(@RequestBody LabelCompanyRequest request) {
        List<Integer> labelIds = companyApiAdminMapper.selectLabelByCompany(LabelApi.LABEL_TYPE_COMPANY, request.getCompanyId());
        List<Integer> authLabelIds = companyApiAdminMapper.selectLabelByCompany(LabelApi.LABEL_TYPE_COMPANY_AUTH, request.getCompanyId());
        LabelCompanyResponse response = LabelCompanyResponse.builder()
                .labelIds(labelIds)
                .authLabelIds(authLabelIds)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation(value = "更新企业标签")
    @PostMapping("company/save")
    @SecurityAuthCheck
    public ResultModel companySave(@RequestBody LabelCompanySaveRequest request) {
        LabelRelateRequest deleteRequest = LabelRelateRequest.builder()
                .type(LabelApi.LABEL_TYPE_COMPANY)
                .typeId(request.getCompanyId())
                .labelIds(request.getLabelIds())
                .build();
        ResultModel resultModel = labelFeign.relate(deleteRequest);
        AssertUtil.isTrue(resultModel);
        return ResultModel.success();
    }

}
